<?php
$error = '';
if(isset($_POST['submit']))
{	
	if($_POST['page_name'] == '' || $_POST['page_content'] == '')
	{
		$error = 'Please fill in the mandatory fields';
	}
	else
	{
		$current_page_name = preg_replace('/(\s)+/','_',$_POST['page_name']); //replace the spaces in page name with underscore
		$query = " SELECT id FROM pages WHERE name = '".$current_page_name."'";
		if(mysql_num_rows(mysql_query($query)) > 0 )
		{
			$error = 'Page name is not available, please choose another one';
		}
		else
		{
			$query = " INSERT INTO pages(name,content,meta_description,meta_keywords,title) VALUES('".$current_page_name."','".$_POST['page_content']."','".$_POST['meta_description']."','".$_POST['meta_keywords']."','".$_POST['page_title']."') ";
			mysql_query($query);
			save_page($current_page_name, $_POST['page_content']);
			header('location:index.php?page=listing&sm=Page added successfully'); exit;
		}
	}
}

?>
<h2>Add Page</h2>
<?php if(!empty($error)) { ?>
<div class="error"><?php echo $error; ?> </div>
<?php } ?>
<form method="post">
<table cellpadding="2" cellspacing="2" border="0" style="background-color:#000000;">
	<tr style="background-color:#FFFFFF;">
		<td>Page Name<span style="color:F0F0F0;"> *</span></td><td><input type="text" name="page_name" /></td>
	</tr>
	<tr style="background-color:#FFFFFF;">		
		<td>Page Content<span style="color:F0F0F0;"> *</span></td><td><textarea cols="20" rows="" name="page_content"></textarea></td>			
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td>Meta Description</td><td><textarea cols="20" rows="" name="meta_description"></textarea></td>
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td>Meta Keywords</td><td><textarea cols="20" rows="" name="meta_keywords"></textarea></td>			
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td>Title</td><td><input type="text" name="page_title" /></td>			
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td colspan="2"><input type="submit" name="submit" value="Save" /></td>			
	</tr>
</table>	
</form>	